const app = getApp()

Page({

  data: {
    rotateDed: 0,
    deg: 720,
    allNum: 8,
    index: 1,
    hasTrans: true,
    animating: false,
    timerArr: [],
    scrollNum: 0,
    list: [],
    into: "item0",
    prize: {
      1: '50金币',
      2: '100金币',
      3: '50元百度糯米券',
      4: 'CHERRY机械键盘',
      5: '200金币',
      6: '500金币',
      7: '100元现金红包',
      8: '小米AI音响'
    },
    indexArr: [1, 2, 5, 6]
  },

  onShow: function() {
    this.setData({
      gold: app.globalData.gold,
    })
  },

  start: function() {
    // 处理金币
    if (app.globalData.gold < 200) {
      wx.showToast({
        title: '金币不足',
        icon: "none"
      })
      return
    }
    app.globalData.gold -= 200
    wx.setStorageSync('baibao_userinfo_gold', app.globalData.gold)
    this.setData({
      gold: app.globalData.gold
    })

    var that = this
    var indexArr = that.data.indexArr
    var index = indexArr[Math.floor(Math.random() * indexArr.length)]
    if (!that.data.animating) {
      that.setData({
        rotateDed: 0
      })
      var i = 360 / that.data.allNum
      var n = .25 * i + Math.random() * i / 2
      var time1 = setTimeout(function() {
        var timeArr = []
        timeArr.push(time1)
        that.setData({
          hasTrans: true,
          animating: true,
          showGif: true,
          index: index,
          rotateDed: that.data.deg + (that.data.allNum - index) * i + n,
          timerArr: that.data.timerArr.concat(timeArr)
        })
        clearTimeout(time1)
      }, 200)
      var time2 = setTimeout(function() {
        var timeArr = []
        timeArr.push(time2)
        that.setData({
          showGif: false,
          hasTrans: false,
          animating: false,
          showDetail: true,
          index: index,
          timerArr: that.data.timerArr.concat(timeArr)
        })
        that.handle_add_gold(index)        
        clearTimeout(time2)
      }, 8200)
    }
  },

  handle_add_gold: function (index) {
    var gold = 0
    switch (index) {
      case 1:
        gold = 50
        break
      case 2:
        gold = 100
        break
      case 5:
        gold = 200
        break
      case 6:
        gold = 500
        break
    }
    app.globalData.gold += gold
    wx.setStorageSync('baibao_userinfo_gold', app.globalData.gold)
    this.setData({
      gold: app.globalData.gold
    })
  },

  know: function() {
    for (var a = 0; a < this.data.timerArr.length; a++) {
      clearTimeout(this.data.timerArr[a])
      clearInterval(this.data.timerArr[a])
    }

    this.setData({
      timerArr: [],
      showDetail: false
    })
  },
})